package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        // 我们需要一个工具类 DigestUtils 对密码进行 加密
        //String s = DigestUtils.md5DigestAsHex(password.getBytes());
       password = DigestUtils.md5DigestAsHex(password.getBytes());

        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }
    //新增员工     的service
    // 把我们得到的对象变成 用对应的实体类封装，传递到下一层
    public void save(EmployeeDTO employeeDTO) {//当传入的数据是dto，
        // 但是数据库需要的是实体类，所以需要把dto变成实体类，且两种类型之间的差距是很大的
        Employee employee = new Employee();
        //对象属性的拷贝，就是把我们拿到的对象
        BeanUtils.copyProperties(employeeDTO,employee);

        //设置账号的状态
        employee.setStatus(StatusConstant.ENABLE);



        //设置默认的密码。需要进行加密
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
         //
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(BaseContext.getCurrentId());//动态的设置为 用户的id
        employee.setUpdateUser(BaseContext.getCurrentId());

        //设置创建的时间和修改的时间
        employeeMapper.insert(employee);


    }
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        // 我想获取那一页的数据 和每一页对应的数据数量

    Page<Employee> page=employeeMapper.pageQuery(employeePageQueryDTO);// page 类型的对象去接
        long total = page.getTotal();
        List<Employee> records = page.getResult();
        return new PageResult(total,records);


    }
    //启用禁用员工账号
    public void startOrStop(Integer status,long id){
        Employee employee = new Employee();
        employee.setStatus(status);
        employee.setId(id);
        employeeMapper.update(employee);


    }
    //编辑员工的信息
    public void edit(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();//我们就是把我们的实体类型转化为employee
        BeanUtils.copyProperties(employeeDTO,employee);//进行一个拷贝构造 将我们的dto转化为employee
        //默认的调用我们的日期转化器
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(BaseContext.getCurrentId());//获取并更改修改者的id
        employeeMapper.update(employee);//更新员工信息哪里不为空就更新 不如就是更新操作也是编辑操作

    }
    //根据id查询员工信息
    public Employee getById(int id){//通过我们的id 查询出来我们对应的员工
        Employee employee = employeeMapper.getById(id);
        employee.setPassword("*****");
        return employee;//返回

    }


}
